package com.ruoyi.servie.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.ruoyi.servie.param.AliyunBatchSmsParam;
import com.ruoyi.servie.param.AliyunSmsParam;
import com.ruoyi.servie.service.IAliyunSmsService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class AliyunSmsServiceImpl implements IAliyunSmsService {


    @Override
    public CommonResponse sendSms(AliyunSmsParam aliyunSmsParam) {

        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", aliyunSmsParam.getAccessKeyId(), aliyunSmsParam.getAccessKeySecret());
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        //默认参数无需修改
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");

        //自定义参数
        //接收短信的手机号码。
        List<String> phoneNumbers = aliyunSmsParam.getPhoneNumbers();
        StringBuffer sbphoneNumber = new StringBuffer();
        for (String phoneNumber: phoneNumbers) {
            sbphoneNumber.append(phoneNumber+",");
        }
        request.putQueryParameter("PhoneNumbers", sbphoneNumber.substring(0,sbphoneNumber.length()-1));
        //短信签名名称
        request.putQueryParameter("SignName", aliyunSmsParam.getSignName());
        //短信模板ID
        request.putQueryParameter("TemplateCode", aliyunSmsParam.getTemplateCode());

        Map<String, Object> templateParam = aliyunSmsParam.getTemplateParam();
        String templateParamJson = JSON.toJSONString(templateParam);
        //模板参数值
        request.putQueryParameter("TemplateParam", templateParamJson);
        CommonResponse response = new CommonResponse();
        try {
            response = client.getCommonResponse(request);
            //{"Message":"OK","RequestId":"1E3CD1E7-5A0E-49E3-B505-C3A46287FDC1","BizId":"582902673572967005^0","Code":"OK"}
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return response;
    }



    @Override
    public CommonResponse sendBatchSms(AliyunBatchSmsParam aliyunBatchSmsParam) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", aliyunBatchSmsParam.getAccessKeyId(), aliyunBatchSmsParam.getAccessKeySecret());
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");

        //自定义参数
        //接收短信的手机号码，JSON数组格式。["15900000000","13500000000"]
        List<String> phoneNumbers = aliyunBatchSmsParam.getPhoneNumbers();
        JSONArray objectsphoneNumbers = JSONArray.parseArray(JSON.toJSONString(phoneNumbers));
        request.putQueryParameter("PhoneNumberJson",objectsphoneNumbers.toString());

        //短信签名名称，JSON数组格式。["阿里云","阿里巴巴"]
        List<String> signNames = aliyunBatchSmsParam.getSignNames();
        JSONArray objectssignNames = JSONArray.parseArray(JSON.toJSONString(phoneNumbers));
        request.putQueryParameter("SignNameJson",objectssignNames.toString());

        //短信模板CODE
        request.putQueryParameter("TemplateCode",aliyunBatchSmsParam.getTemplateCode());

        //短信模板变量对应的实际值，JSON格式。[{"name":"TemplateParamJson"},{"name":"TemplateParamJson"}]
        List<Map<String, Object>> templateParam = aliyunBatchSmsParam.getTemplateParam();
        StringBuffer sbtemplateParam = new StringBuffer();
        sbtemplateParam.append("[");
        for (Map<String, Object> map :templateParam) {
            String templateParamJson = JSON.toJSONString(map);
            sbtemplateParam.append(templateParamJson+",");
        }
        String stemplateParam = sbtemplateParam.substring(0, sbtemplateParam.length() - 1)+"]";
        request.putQueryParameter("TemplateParamJson",stemplateParam);

        CommonResponse response = new CommonResponse();
        try {
            response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return response;
    }
}
